package FileInformation.helpers;

import FileInformation.utils.EndOfLineType;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: Nobody
 * Date: 25.04.11
 * Time: 21:28
 * To change this template use File | Settings | File Templates.
 */
public class EndOfLineChecker extends AbstractFileHelper implements Serializable{
    private static final int carriageReturnMark = 13;
    private static final int endOfLineMark = 10;
    private EndOfLineType endLineType;

    public EndOfLineType getEndLineType() {
        return endLineType;
    }

    public void setEndLineType(EndOfLineType endLineType) {
        this.endLineType = endLineType;
    }

    public EndOfLineChecker(File file) {
        super(file);
    }

    @Override
    public void execute() throws IOException {
        FileInputStream stream = new FileInputStream(file);
        byte[] buffer = new byte[2];
        while (stream.read(buffer) > 0) {
                if (buffer[1] == endOfLineMark) {
                    if (buffer[0] == carriageReturnMark) {
                        endLineType = EndOfLineType.WINDOWS;
                        stream.close();
                        return;
                    } else {
                        endLineType = EndOfLineType.LINUX;
                        stream.close();
                        return;
                }
            }
        }
        stream.close();
    }
}
